<template>
  <el-card shadow="never" :body-style="{ padding: '20px' }">
    <el-button class="addBtn" type="primary" size="default" icon="Plus" @click="addTrademark">添加品牌</el-button>
    <el-table :data="trademarkArr" style="width:100%;margin-bottom:20px" border>
      <el-table-column label="序号" width="180" type="index" align="center">
      </el-table-column>
      <el-table-column label="品牌名称" width="380" align="center">
        <template #default="scope">
          {{ scope.row.tmName }}
        </template>
      </el-table-column>
      <el-table-column label="品牌LOGO" width="380" align="center">
        <template #default="scope">
          <el-image class="logo" :src="scope.row.logoUrl" fit="fill" :lazy="true"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="品牌操作">
        <template #default="scope">
          <el-button type="warning" @click="updateTrademark">修改</el-button>
          <el-button type="danger" @click="">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 layout 控制分页布局的 ->:把右侧属性挤到组件右侧 total,sizes 总数 和 分页数量选项 在最右侧 -->
    <el-pagination v-model:current-page="currPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 50, 70]" background
      layout="prev, pager, next, jumper,->,total, sizes" :total="total" @size-change="pageSizeChange"
      @current-change="currentChange" />
  </el-card>

  <el-dialog v-model="dialogFormVisible" :title="dialogTitle">
    <el-form :model="form" :rules="rules" style="width:80%" label-position="left">
      <el-form-item label="品牌名称" :label-width="80" prop="tmName">
        <el-input v-model="form.tmName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="品牌LOGO" :label-width="80">
        <el-upload class="avatar-uploader" action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { reqHasTrademark } from '@/api/product/trademark'
import type { TrademarkResponseData, TardeMark } from '@/api/product/trademark/type';
import { FormRules } from 'element-plus';

const currPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const trademarkArr = ref<TardeMark[]>([]);
const dialogFormVisible = ref(false);
const dialogTitle = ref('添加品牌');
const form = reactive({
  tmName: '',
  logoUrl: ''
})

const rules = reactive<FormRules>({
  tmName: [
    { required: true, message: '品牌名称不能为空', trigger: 'blur' },
    { min: 2, message: '品牌名称最少两个字符', trigger: 'blur' },
  ]
})

const getHasTradmark = async () => {
  let res: TrademarkResponseData = await reqHasTrademark(currPage.value, pageSize.value);
  if (res.code == 200) {
    total.value = res.data.total
    trademarkArr.value = res.data.records
  }
}
onMounted(() => {
  getHasTradmark();
})

const pageSizeChange = () => {
  getHasTradmark();
}

const currentChange = () => {
  getHasTradmark();
}

const addTrademark = () => {
  dialogTitle.value = '添加品牌'
  dialogFormVisible.value = true
}
const updateTrademark = () => {
  dialogTitle.value = '修改品牌'
  dialogFormVisible.value = true
}

</script>

<style scoped lang="scss">
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.addBtn {
  margin-bottom: 20px;
}

.logo {
  width: 100px;
  height: 100px;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>